home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-desktop-9.10-i386-PL.iso / casper / filesystem.squashfs / usr / lib / python2.6 / rlcompleter.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2009-11-11  |  6KB  |  195 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. '''Word completion for GNU readline 2.0.
  5.  
  6. This requires the latest extension to the readline module. The completer
  7. completes keywords, built-ins and globals in a selectable namespace (which
  8. defaults to __main__); when completing NAME.NAME..., it evaluates (!) the
  9. expression up to the last dot and completes its attributes.
  10.  
  11. It\'s very cool to do "import sys" type "sys.", hit the
  12. completion key (twice), and see the list of names defined by the
  13. sys module!
  14.  
  15. Tip: to use the tab key as the completion key, call
  16.  
  17.     readline.parse_and_bind("tab: complete")
  18.  
  19. Notes:
  20.  
  21. - Exceptions raised by the completer function are *ignored* (and
  22. generally cause the completion to fail).  This is a feature -- since
  23. readline sets the tty device in raw (or cbreak) mode, printing a
  24. traceback wouldn\'t work well without some complicated hoopla to save,
  25. reset and restore the tty state.
  26.  
  27. - The evaluation of the NAME.NAME... form may cause arbitrary
  28. application defined code to be executed if an object with a
  29. __getattr__ hook is found.  Since it is the responsibility of the
  30. application (or the user) to enable this feature, I consider this an
  31. acceptable risk.  More complicated expressions (e.g. function calls or
  32. indexing operations) are *not* evaluated.
  33.  
  34. - GNU readline is also used by the built-in functions input() and
  35. raw_input(), and thus these also benefit/suffer from the completer
  36. features.  Clearly an interactive application can benefit by
  37. specifying its own completer function and using raw_input() for all
  38. its input.
  39.  
  40. - When the original stdin is not a tty device, GNU readline is never
  41. used, and this module (and the readline module) are silently inactive.
  42.  
  43. '''
  44. import __builtin__
  45. import __main__
  46. __all__ = [
  47.     'Completer']
  48.  
  49. class Completer:
  50.     
  51.     def __init__(self, namespace = None):
  52.         '''Create a new completer for the command line.
  53.  
  54.         Completer([namespace]) -> completer instance.
  55.  
  56.         If unspecified, the default namespace where completions are performed
  57.         is __main__ (technically, __main__.__dict__). Namespaces should be
  58.         given as dictionaries.
  59.  
  60.         Completer instances should be used as the completion mechanism of
  61.         readline via the set_completer() call:
  62.  
  63.         readline.set_completer(Completer(my_namespace).complete)
  64.         '''
  65.         if namespace and not isinstance(namespace, dict):
  66.             raise TypeError, 'namespace must be a dictionary'
  67.         not isinstance(namespace, dict)
  68.         if namespace is None:
  69.             self.use_main_ns = 1
  70.         else:
  71.             self.use_main_ns = 0
  72.             self.namespace = namespace
  73.  
  74.     
  75.     def complete(self, text, state):
  76.         """Return the next possible completion for 'text'.
  77.  
  78.         This is called successively with state == 0, 1, 2, ... until it
  79.         returns None.  The completion should begin with 'text'.
  80.  
  81.         """
  82.         if self.use_main_ns:
  83.             self.namespace = __main__.__dict__
  84.         
  85.         if state == 0:
  86.             if '.' in text:
  87.                 self.matches = self.attr_matches(text)
  88.             else:
  89.                 self.matches = self.global_matches(text)
  90.         
  91.         
  92.         try:
  93.             return self.matches[state]
  94.         except IndexError:
  95.             return None
  96.  
  97.  
  98.     
  99.     def _callable_postfix(self, val, word):
  100.         if hasattr(val, '__call__'):
  101.             word = word + '('
  102.         
  103.         return word
  104.  
  105.     
  106.     def global_matches(self, text):
  107.         '''Compute matches when text is a simple name.
  108.  
  109.         Return a list of all keywords, built-in functions and names currently
  110.         defined in self.namespace that match.
  111.  
  112.         '''
  113.         import keyword as keyword
  114.         matches = []
  115.         n = len(text)
  116.         for word in keyword.kwlist:
  117.             if word[:n] == text:
  118.                 matches.append(word)
  119.                 continue
  120.         
  121.         for nspace in [
  122.             __builtin__.__dict__,
  123.             self.namespace]:
  124.             for word, val in nspace.items():
  125.                 if word[:n] == text and word != '__builtins__':
  126.                     matches.append(self._callable_postfix(val, word))
  127.                     continue
  128.             
  129.         
  130.         return matches
  131.  
  132.     
  133.     def attr_matches(self, text):
  134.         '''Compute matches when text contains a dot.
  135.  
  136.         Assuming the text is of the form NAME.NAME....[NAME], and is
  137.         evaluatable in self.namespace, it will be evaluated and its attributes
  138.         (as revealed by dir()) are used as possible completions.  (For class
  139.         instances, class members are also considered.)
  140.  
  141.         WARNING: this can still invoke arbitrary C code, if an object
  142.         with a __getattr__ hook is evaluated.
  143.  
  144.         '''
  145.         import re as re
  146.         m = re.match('(\\w+(\\.\\w+)*)\\.(\\w*)', text)
  147.         if not m:
  148.             return []
  149.         (expr, attr) = m.group(1, 3)
  150.         
  151.         try:
  152.             thisobject = eval(expr, self.namespace)
  153.         except Exception:
  154.             m
  155.             m
  156.             return []
  157.  
  158.         words = dir(thisobject)
  159.         if '__builtins__' in words:
  160.             words.remove('__builtins__')
  161.         
  162.         if hasattr(thisobject, '__class__'):
  163.             words.append('__class__')
  164.             words.extend(get_class_members(thisobject.__class__))
  165.         
  166.         matches = []
  167.         n = len(attr)
  168.         for word in words:
  169.             if word[:n] == attr and hasattr(thisobject, word):
  170.                 val = getattr(thisobject, word)
  171.                 word = self._callable_postfix(val, '%s.%s' % (expr, word))
  172.                 matches.append(word)
  173.                 continue
  174.         
  175.         return matches
  176.  
  177.  
  178.  
  179. def get_class_members(klass):
  180.     ret = dir(klass)
  181.     if hasattr(klass, '__bases__'):
  182.         for base in klass.__bases__:
  183.             ret = ret + get_class_members(base)
  184.         
  185.     
  186.     return ret
  187.  
  188.  
  189. try:
  190.     import readline
  191. except ImportError:
  192.     pass
  193.  
  194. readline.set_completer(Completer().complete)
  195.